from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import User
from django.db import models
from django.dispatch import receiver
from django.db.models.signals import post_save

# Create your models here.


class UserExtension(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='extension')
    identity = models.CharField(max_length=100, blank=True, null=True)
    user_uid = models.IntegerField(blank=True, null=True)


@receiver(post_save, sender=User)
def create_user_extension(sender, instance, created, **kwargs):
    if created:
        UserExtension.objects.create(user=instance)
    else:
        instance.extension.save()


class Appeal(models.Model):
    appeal_num = models.AutoField(primary_key=True)
    pub_pub_uid = models.ForeignKey('Publisher', models.DO_NOTHING, db_column='pub_pub_uid', blank=True, null=True)
    sta_staff_uid = models.ForeignKey('Staff', models.DO_NOTHING, db_column='sta_staff_uid', blank=True, null=True)
    pub_uid = models.IntegerField(blank=True, null=True)
    task_num = models.IntegerField(blank=True, null=True)
    appeal_text = models.TextField(blank=True, null=True)
    staff_uid = models.IntegerField(blank=True, null=True)
    deal_text = models.TextField(blank=True, null=True)
    deal_state = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'appeal'


class Checktask(models.Model):
    task_num = models.IntegerField(primary_key=True)
    receiver_uid = models.IntegerField()
    mis_task_num = models.ForeignKey('Mission', models.DO_NOTHING, db_column='mis_task_num', blank=True, null=True, related_name='mis_task_num_1')
    mis_task_num2 = models.ForeignKey('Mission', models.DO_NOTHING, db_column='mis_task_num2', blank=True, null=True, related_name='mis_task_num_2')
    sta_staff_uid = models.ForeignKey('Staff', models.DO_NOTHING, db_column='sta_staff_uid', blank=True, null=True)
    rec_receiver_uid = models.ForeignKey('Receiver', models.DO_NOTHING, db_column='rec_receiver_uid', blank=True, null=True)
    mis_task_num3 = models.ForeignKey('Mission', models.DO_NOTHING, db_column='mis_task_num3', blank=True, null=True, related_name='mis_task_num_3')
    mis_task_num4 = models.ForeignKey('Mission', models.DO_NOTHING, db_column='mis_task_num4', blank=True, null=True, related_name='mis_task_num_4')
    check_amount = models.IntegerField(blank=True, null=True)
    checkdata_num = models.CharField(max_length=1024, blank=True, null=True)
    staff_uid = models.IntegerField(blank=True, null=True)
    subtask_acc = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'checktask'
        unique_together = (('task_num', 'receiver_uid'),)


class Mission(models.Model):
    task_num = models.AutoField(primary_key=True)
    order_num = models.ForeignKey('Taskorder', models.DO_NOTHING, db_column='order_num', blank=True, null=True)
    pub_pub_uid = models.ForeignKey('Publisher', models.DO_NOTHING, db_column='pub_pub_uid', blank=True, null=True)
    tas_task_num = models.ForeignKey('Tasktaxt', models.DO_NOTHING, db_column='tas_task_num', blank=True, null=True)
    task_name = models.CharField(max_length=50, blank=True, null=True)
    task_time = models.DateTimeField(blank=True, null=True)
    pub_uid = models.IntegerField(blank=True, null=True)
    task_datasize = models.IntegerField(blank=True, null=True)
    lable_type = models.CharField(max_length=50, blank=True, null=True)
    customize_demand = models.TextField(blank=True, null=True)
    task_reward = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
    rec_limitation = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
    storage_url = models.CharField(max_length=50, blank=True, null=True)
    publish_state = models.IntegerField(blank=True, null=True)
    claim_state = models.IntegerField(blank=True, null=True)
    task_complete_state = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'mission'


class Publisher(models.Model):
    pub_uid = models.AutoField(primary_key=True)
    pub_username = models.CharField(max_length=20, blank=True, null=True)
    password = models.CharField(max_length=20, blank=True, null=True)
    phonenum = models.CharField(max_length=11, blank=True, null=True)
    pub_account = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
    vip_state = models.IntegerField(blank=True, null=True)
    vip_uid = models.CharField(max_length=10, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'publisher'


class Pubtext(models.Model):
    pub_uid = models.IntegerField(primary_key=True)
    content = models.TextField(blank=True, null=True)
    read_state = models.IntegerField(blank=True, null=True)
    establish_time = models.DateTimeField()
    staff_uid = models.ForeignKey('Staff', models.DO_NOTHING, db_column='staff_uid', blank=True, null=True)
    receiver_uid = models.ForeignKey('Receiver', models.DO_NOTHING, db_column='receiver_uid', blank=True, null=True)
    pub_pub_uid = models.ForeignKey(Publisher, models.DO_NOTHING, db_column='pub_pub_uid', blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'pubtext'
        unique_together = (('pub_uid', 'establish_time'),)


class Receiver(models.Model):
    receiver_uid = models.AutoField(primary_key=True)
    rec_username = models.CharField(max_length=20, blank=True, null=True)
    password = models.CharField(max_length=20, blank=True, null=True)
    phonenum = models.CharField(max_length=11, blank=True, null=True)
    rec_account = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
    history_tag_ammount = models.IntegerField(blank=True, null=True)
    acc_score = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
    punish_record = models.IntegerField(blank=True, null=True)
    punish_state = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'receiver'


class Rectext(models.Model):
    receiver_uid = models.IntegerField(primary_key=True)
    content = models.TextField(blank=True, null=True)
    read_state = models.IntegerField(blank=True, null=True)
    establish_time = models.DateTimeField()
    rec_receiver_uid = models.ForeignKey(Receiver, models.DO_NOTHING, db_column='rec_receiver_uid', blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'rectext'
        unique_together = (('receiver_uid', 'establish_time'),)


class Staff(models.Model):
    staff_uid = models.AutoField(primary_key=True)
    staff_username = models.CharField(max_length=20, blank=True, null=True)
    password = models.CharField(max_length=20, blank=True, null=True)
    staff_num = models.CharField(max_length=11, blank=True, null=True)
    phonenum = models.CharField(max_length=11, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'staff'


class Statext(models.Model):
    staff_uid = models.IntegerField(primary_key=True)
    content = models.TextField(blank=True, null=True)
    read_state = models.IntegerField(blank=True, null=True)
    establish_time = models.DateTimeField()
    sta_staff_uid = models.ForeignKey(Staff, models.DO_NOTHING, db_column='sta_staff_uid', blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'statext'
        unique_together = (('staff_uid', 'establish_time'),)


class Taskalllocate(models.Model):
    task_num = models.IntegerField(primary_key=True)
    receiver_uid = models.IntegerField()
    mis_task_num = models.ForeignKey(Mission, models.DO_NOTHING, db_column='mis_task_num', blank=True, null=True)
    rec_receiver_uid = models.ForeignKey(Receiver, models.DO_NOTHING, db_column='rec_receiver_uid', blank=True, null=True)
    received_task_amount = models.IntegerField(blank=True, null=True)
    subtask_acc = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
    subtask_complete_state = models.IntegerField(blank=True, null=True)
    subtask_generate_time = models.DateField(blank=True, null=True)
    subtask_data_begin_num = models.IntegerField(blank=True, null=True)
    subtask_current_cursor = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'taskalllocate'
        unique_together = (('task_num', 'receiver_uid'),)


class Taskorder(models.Model):
    order_num = models.IntegerField(primary_key=True)
    mis_task_num = models.ForeignKey(Mission, models.DO_NOTHING, db_column='mis_task_num', blank=True, null=True)
    task_num = models.IntegerField(blank=True, null=True)
    order_reward = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
    paid_state = models.IntegerField(blank=True, null=True)
    order_complete_state = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'taskorder'


class Tasktaxt(models.Model):
    task_num = models.IntegerField(primary_key=True)
    mis_task_num = models.ForeignKey(Mission, models.DO_NOTHING, db_column='mis_task_num', blank=True, null=True)
    sub_storage_url = models.CharField(max_length=50, blank=True, null=True)
    task_data_num = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tasktaxt'
